package com.biblum.topgas;

import java.util.List;

import android.R.drawable;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.biblum.topgas.R;
import com.google.ads.AdView;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
import com.google.android.maps.MyLocationOverlay;
import com.google.android.maps.Overlay;


public class TopGasMapActivity extends MapActivity {
	
	public static final int MENU_MY_LOCATION = 1;
	
	MapView mapView;
	TopGasOverlay topGasOverLay;
	EstacionesData estacionesData;
	MyLocationOverlay myLocationOverlay;
	boolean detectLocation;
	
	public void onCreate(Bundle icicle) {
        super.onCreate(icicle);

		// No Titlebar
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		requestWindowFeature(Window.FEATURE_PROGRESS);
 	    
	    setContentView(R.layout.topgas_map);

	    // Look up the AdView as a resource and load a request.
		MyAdManager.initialize(this);
	    MyAdManager.loadAd((AdView)this.findViewById(R.id.adView));
	    
        mapView = (MapView)findViewById(R.id.mapview);
        mapView.setBuiltInZoomControls(true);
    	mapView.getController().setZoom(15);
        
        List<Overlay> overlays = mapView.getOverlays();
        myLocationOverlay = new MyLocationOverlay(this, mapView);
        overlays.add(myLocationOverlay);

        String elTitulo = "Mapa";
	    Bundle bundle = getIntent().getExtras();	
	    if ( bundle != null )
	    {
		    elTitulo = bundle.getString(Constantes.ATRIBUTO_TITULO);
	    }
	    else
	    {
	    	if ( estacionesData != null )
	    	{
	    		String combustible = estacionesData.getCombustible();		
	    		combustible = Constantes.getDescripcionCombustible(combustible);

	    		elTitulo = "Mapa : " + combustible;
	    	}
	    }
	    
		TextView title = (TextView) this.findViewById(R.id.TituloMapa);
		title.setText(elTitulo);	    
        
        topGasOverLay = new TopGasOverlay(this);
        overlays.add(topGasOverLay);
        
        estacionesData = EstacionesData.getInstance(this);
    }
	
	@Override
	protected void onResume() 
	{
		super.onResume();
		
    	SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this);
		mapView.setSatellite(sharedPrefs.getBoolean("satellite", false));
		boolean fullscreen = sharedPrefs.getBoolean("fullscreen", false);

		// No statusbar
		if (fullscreen) {
			getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
		} else {
			getWindow().setFlags(0, WindowManager.LayoutParams.FLAG_FULLSCREEN);
		}

        myLocationOverlay.enableMyLocation();
        
        if (estacionesData.getSelectedPoint() != null) 
        {        	
        	mapView.getController().setCenter(estacionesData.getSelectedPoint());
        }
        else if (estacionesData.getLocationPoint() != null) 
        {
        	mapView.getController().setCenter(estacionesData.getLocationPoint());
        }
        
	}

	@Override
	protected void onPause() 
	{
		super.onPause();
		// To save battery
		myLocationOverlay.disableMyLocation(); // VEry important, if not, countinues to use the GPS
	}
	
	@Override
	protected boolean isRouteDisplayed() 
	{
		return false;
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) 
	{
		super.onCreateOptionsMenu(menu);

		menu.add(0, MENU_MY_LOCATION, 0, R.string.menu_my_location).setIcon(drawable.ic_menu_mylocation);
		
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		GeoPoint geoPoint = null;
		switch (item.getItemId()) 
		{
		case MENU_MY_LOCATION:
			geoPoint = myLocationOverlay.getMyLocation();
			// Fallback
			if (geoPoint == null) 
				geoPoint = estacionesData.getLocationPoint();
    		if (geoPoint != null) 
    			mapView.getController().animateTo(geoPoint);
			return true;
		}
		return false;
	}

	public void onBackAction(View v) {
		finish();
	}
	
	public void onPreferencesAction(View v) {
		Intent intent = new Intent(getApplicationContext(), PreferencesActivity.class);
		startActivity(intent);
	}
}